<template>
    <div class="mod-prod">
        <avue-crud ref="crud"
                   :data="dataList"
                   :option="tableOption"
                   :page="page"
                   :permission="permission"
                   :table-loading="dataListLoading"
                   @search-change="searchChange"
                   @selection-change="selectionChange"
                   @on-load="getDataList"
                   @row-save="sourceRowSave"
                   @refresh-change="getDataList(page)"
        >

            <template slot="menuLeft">
                <el-button type="primary"
                           icon="el-icon-plus"
                           size="small"
                           @click.stop="addOrUpdateHandle()">新增</el-button>
<!--              <el-button type="primary"-->
<!--                         icon="el-icon-edit"-->
<!--                         size="small"-->
<!--                         @click.stop="addOrUpdateHandle()">修改</el-button>-->
<!--                <el-button type="danger"-->
<!--                           icon="el-icon-remove"-->
<!--                           @click="deleteHandle()"-->
<!--                           size="small"-->
<!--                           :disabled="dataListSelections.length <= 0">删除</el-button>-->
            </template>
            <template slot="cusStatus"
                      slot-scope="scope">
                <el-tag v-if="scope.row.cusStatus === '1'"
                        size="small">未分配
                </el-tag>
                <el-tag v-else-if="scope.row.cusStatus === '2'"
                        size="small">跟踪中
                </el-tag>
                <el-tag v-else-if="scope.row.cusStatus === '3'"
                        size="small">已转化
                </el-tag>
                <el-tag v-else-if="scope.row.cusStatus === '4'"
                        size="small">已签单
                </el-tag>
            </template>

            <template slot="menu"
                      slot-scope="scope">

<!--                <el-button type="danger"-->
<!--                           size="small"-->
<!--                           @click="openGzDialog(scope.row.id)"-->
<!--                >人员分配</el-button>-->
                <el-button type="danger"
                           size="small"
                           @click="deleteOppo(scope.row.id)"
                >删除</el-button>
                <el-button type="primary"
                           size="small"
                           @click="getMyself(scope.row)"
                >领到私海</el-button>
                <el-button type="primary"
                           size="small"
                           @click="openGzDialog(scope.row.id)"
                >记录</el-button>
              <el-button type="primary"
                         size="small"
                         @click="traceCustInfo(scope.row)"
              >跟进</el-button>
            </template>
        </avue-crud>
        <!-- 弹窗, 新增 / 修改 -->
        <add-or-update v-if="addOrUpdateVisible"
                       ref="addOrUpdate"
                       @refreshDataList="getDataList"></add-or-update>
        <oppo-next-list ref="oppoNextListRef"></oppo-next-list>
        <oppo-trace ref="oppoTraceRef" @refreshDataList="getDataList(page)"></oppo-trace>
    </div>
</template>

<script>
import {tableOption} from '@/crud/relationship/puboppo'
import AddOrUpdate from './pub-oppo-add-or-update.vue'
import OppoNextList from './oppo-next-list.vue'
import OppoTrace from "@/views/modules/relationship/oppo-trace";
export default {
    components: {
        AddOrUpdate,
        OppoNextList,
      OppoTrace
    },
	data () {
		return {
            addOrUpdateVisible: false,
			dataForm: {
				prodName: ''
			},
			permission: {
				delBtn: this.isAuth('prod:prod:delete')
			},
			dataList: [],
			page: {
				total: 0, // 总页数
				currentPage: 1, // 当前页数
                pageSize: 1000, // 每页显示多少条
                pageSizes: [10,50,100,500,1000]
			},
			dataListSelections: [],
			dataListLoading: false,
			tableOption: tableOption,
			resourcesUrl: process.env.VUE_APP_RESOURCES_URL
		}
	},
    mounted() {
        this.initData()
    },
	methods: {
        initData() {
            // 初始化字典：行业、等级等（保持不变）
            Promise.all([
                this.$http({
                    url: this.$http.adornUrl('/crm/base/cusIndustry/page'),
                    method: 'get',
                    params: this.$http.adornParams({ current: 1, size: 99 })
                }),
            ]).then(([industryRes, levelRes]) => {
                const industryCol = this.tableOption.column.find(i => i.prop === 'biz6');
                if (industryCol) {
                    industryCol.dicData = industryRes.data.obj.records.map(i => ({
                        label: i.industryName,
                        value: i.industryName
                    }));
                }


            });
        },
		// 获取数据列表
		getDataList (page, params, done) {
			this.dataListLoading = true
			this.$http({
				url: this.$http.adornUrl('/crm/oppo/page'),
				method: 'get',
				params: this.$http.adornParams(
					Object.assign(
						{
							current: page == null ? this.page.currentPage : page.currentPage,
							size: page == null ? this.page.pageSize : page.pageSize,
                            busiOppoType: "公海"
						},
						params
					)
				)
			}).then(({data}) => {
				this.dataList = data.obj.records
				this.page.total = data.obj.total
				this.dataListLoading = false
				if (done) {
					done()
				}
			})
		},
        openGzDialog(id){
            this.$refs.oppoNextListRef.init(id)
        },
    traceCustInfo(row){
		  console.info(row);
      this.$refs.oppoTraceRef.init(row.id)
    },
		// 新增 / 修改
		addOrUpdateHandle (id) {
            this.addOrUpdateVisible = true
            this.$nextTick(() => {
                this.$refs.addOrUpdate.init(id)
            })
		},
        getMyself(row){
            this.$confirm('确定要领入私海吗？', '友情提醒', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.$http({
                    url: this.$http.adornUrl('/crm/oppo/getPrivateBusiOppo'),
                    method: 'post',
                    data: this.$http.adornData({
                        ...row
                    })
                }).then(({data}) => {
                    this.$message({
                        message: '操作成功',
                        type: 'success',
                        duration: 1500,
                        onClose: () => {
                            this.getDataList(this.page)
                        }
                    })
                })
            }).catch(() => {
            })
        },
        genCustInfo(row){
            this.$confirm('确定进行转正式客户操作?', '友情提醒', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.$http({
                    url: this.$http.adornUrl('/crm/oppo/genCustInfo'),
                    method: 'post',
                    data: this.$http.adornData({
                        ...row
                    })
                }).then(({data}) => {
                    this.$message({
                        message: '操作成功',
                        type: 'success',
                        duration: 1500,
                        onClose: () => {
                            this.getDataList()
                        }
                    })
                })
            }).catch(() => {
            })
        },
        deleteOppo(id){
            this.$confirm(`确定进行删除操作?`, '友情提醒', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            })
                .then(() => {
                    this.$http({
                        url: this.$http.adornUrl(`/crm/oppo/` + id),
                        method: 'delete',
                        data: this.$http.adornData({id: id}, false)
                    }).then(({data}) => {
                        this.$message({
                            message: '操作成功',
                            type: 'success',
                            duration: 1500,
                            onClose: () => {
                                this.getDataList(this.page)
                            }
                        })
                    })
                })
                .catch(() => {
                })
        },
		// 删除和批量删除
		deleteHandle (id) {
			let prodIds = this.getSeleProdIds()
			if (id) {
				prodIds.push(id)
			}
			this.$confirm(`确定进行[${id ? '删除' : '批量删除'}]操作?`, '友情提醒', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning'
			})
				.then(() => {
					this.$http({
						url: this.$http.adornUrl(`/crm/oppo/` + id),
						method: 'delete',
						data: this.$http.adornData(prodIds, false)
					}).then(({data}) => {
						this.$message({
							message: '操作成功',
							type: 'success',
							duration: 1500,
							onClose: () => {
								this.getDataList(this.page)
							}
						})
					})
				})
				.catch(() => {
				})
		},
		rowDel: function (row, index) {
			this.$confirm('确定进行删除操作?', '友情提醒', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning'
			}).then(() => {
				this.$http({
					url: this.$http.adornUrl('/crm/oppo/' + row.id),
					method: 'delete',
					data: this.$http.adornData({})
				}).then(({data}) => {
					this.$message({
						message: '操作成功',
						type: 'success',
						duration: 1500,
						onClose: () => {
							this.getDataList()
						}
					})
				})
			}).catch(() => {
			})
		},
		sourceRowSave (form, done, loading) {
			console.log('🚀 ~ form', form)
			this.$http({
				url: this.$http.adornUrl('/crm/base/prod/saveProd'),
				method: 'post',
				data: this.$http.adornData({
					prodId: wfy.uuid(20),
					prodCode: form.prodCode,
					prodName: form.prodName,
					prodSpec: form.prodSpec,
					prodLenwidth: form.prodLenwidth,
				})
			}).then(({data}) => {
				this.$message({
					message: '操作成功',
					type: 'success',
					duration: 1500,
					onClose: () => {
						if (done) {
							done()
						}
						this.getDataList(this.page)
					}
				})
			})
		},
		// 条件查询
		searchChange (params, done) {
			this.getDataList(this.page, params, done)
		},
		// 多选变化
		selectionChange (val) {
			this.dataListSelections = val
		},
		// 获取选中的商品Id列表
		getSeleProdIds () {
			return this.dataListSelections.map(item => {
				return item.id
			})
		}
	}
}
</script>
<style lang="scss" scoped>
/* 解决序号列与内容列因换行导致的错位 */
::v-deep .avue-crud__index {
    display: table-cell !important; /* 强制序号列使用表格单元格布局 */
    vertical-align: middle !important; /* 内容垂直居中，与数据列对齐 */
    height: 60px !important; /* 取消固定高度，随内容行高自适应 */
}

/* 确保表格行高统一由内容决定 */
::v-deep .el-table__row {
    height: 60px !important;
}

::v-deep .el-table__cell {
    padding: 0px 0 !important; /* 统一单元格内边距，避免行高差异 */
}
</style>
